package cn.innosmart.aq.customize;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhy.autolayout.AutoLayoutInfo;
import com.zhy.autolayout.utils.AutoLayoutHelper;

import java.util.ArrayList;
import java.util.List;

import cn.innosmart.aq.R;

/**
 * Created by sabonis on 6/25/15.
 * With Clean Code in mind
 */
public class HorizonSimpleSelector extends FrameLayout {

    private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
    private List<String> items = new ArrayList<>();
    private int index;
    private TextView mTvLeft;
    private TextView mTvRight;
    private AdapterView.OnItemSelectedListener mOnItemSelectedListener;
    private Context mContext;

    public HorizonSimpleSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.widget_horizon_selector, this, true);
    }

    public HorizonSimpleSelector(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.widget_horizon_selector, this, true);
    }

    public HorizonSimpleSelector(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.widget_horizon_selector, this, true);
    }

    public List<String> getItems() {
        return items;
    }


    public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener onItemSelectedListener) {
        mOnItemSelectedListener = onItemSelectedListener;
        init();
    }

    private void init() {
        mTvLeft = (TextView) findViewById(R.id.tv_left);
        mTvRight = (TextView) findViewById(R.id.tv_right);
        findViewById(R.id.btn_left).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemSelectedListener.onItemSelected(null, getRootView(), -1, 0);
            }
        });

        findViewById(R.id.btn_right).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemSelectedListener.onItemSelected(null, getRootView(), 1, 0);
            }
        });
    }

    public void setText(String left, String right) {
        mTvLeft.setText(left);
        mTvRight.setText(right);
    }

    public void setText(int leftStringId, int rightStringId) {
        mTvLeft.setText(mContext.getString(leftStringId));
        mTvRight.setText(mContext.getString(rightStringId));
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isInEditMode()) {
            mHelper.adjustChildren();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public static class LayoutParams extends FrameLayout.LayoutParams
            implements AutoLayoutHelper.AutoLayoutParams {
        private AutoLayoutInfo mAutoLayoutInfo;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            mAutoLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(int width, int height, int gravity) {
            super(width, height, gravity);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }

        public LayoutParams(FrameLayout.LayoutParams source) {
            super((MarginLayoutParams) source);
            gravity = source.gravity;
        }

        public LayoutParams(LayoutParams source) {
            this((FrameLayout.LayoutParams) source);
            mAutoLayoutInfo = source.mAutoLayoutInfo;
        }

        @Override
        public AutoLayoutInfo getAutoLayoutInfo() {
            return mAutoLayoutInfo;
        }
    }
}
